package com.study.spmongodb.wwj.collections;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/16 23:04
 * https://www.bilibili.com/video/BV1hJ411D7k2?p=154
 */
public class PriorityLinkedList<E extends Comparable<E>> {
    private Node<E> first;
    private final Node<E> NULL = null;
    private final static String PLAIN_NULL = "null";

    private int size;

    private PriorityLinkedList() {
        this.first = NULL;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return (size() == 0);
    }

    public static <E extends Comparable<E>> PriorityLinkedList<E> of(E... elements) {
        final PriorityLinkedList<E> list = new PriorityLinkedList<>();
        if (elements.length != 0) {
            for (E element : elements) {
                list.addFirst(element);
            }
        }
        return list;
    }

    /**
     * 添加元素
     *
     * @param e
     */
    public PriorityLinkedList<E> addFirst(E e) {
        final Node<E> newNode = new Node<>(e);
        Node<E> previous = NULL;
        Node<E> current = this.first;
        while (current != null && e.compareTo(current.value) > 0) {
            previous = current;
            current = current.next;
        }
        if (current == null) {
            first = newNode;
            newNode.next = current;
        } else {
            previous.next = newNode;
            newNode.next = current;
        }
        return this;
    }

    /**
     * 包含
     */
    public boolean contains(E e) {
        final Node<E> current = this.first;
        while (current != null) {
            if (current.value == e) {
                return true;
            }
        }
        return false;
    }

    public E remove() {
        if (this.isEmpty()) {
            throw new LinkedList.NoElementException(" the linked is null");
        }
        final Node<E> node = this.first;
        first = node.next;
        size--;
        return node.value;
    }

    @Override
    public String toString() {
        if (this.isEmpty()) {
            return "[]";
        } else {
            final StringBuilder builder = new StringBuilder("[");
            Node<E> current = this.first;
            while (current != null) {
                builder.append(current.toString()).append(",");
                current = current.next;
            }
            builder.replace(builder.length() - 1, builder.length(), "]");
            return builder.toString();
        }
    }

    static class NoElementException extends RuntimeException {
        public NoElementException(String message) {
            super(message);
        }
    }

    private static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value) {
            this.value = value;
        }

        @Override
        public String toString() {
            if (value != null) {
                return value.toString();
            }
            return PLAIN_NULL;
        }
    }

    public static void main(String[] args) {
        final PriorityLinkedList<Integer> of = PriorityLinkedList.of(-1, 100, 5, 2, 1);
        System.out.println(of);
    }
}
